--每回合限一次，当你装备区内的牌数变化后，你可以视为使用【铁索连环】，
--若此牌仅指定了你或你邻家为目标，本回合你下次需要使用【杀】或【闪】时，可以视为使用之。

local heye = fk.CreateSkill{
  name = "doublez__heye"
}

Fk:loadTranslationTable{
  ["doublez__heye"] = "合野",
  [":doublez__heye"] = "每回合限一次，当你装备区内的牌数变化后，你可以视为使用【铁索连环】，"..
    "若此牌仅指定了你或你邻家为目标，本回合你下次需要使用【杀】或【闪】时，可以视为使用之。",

  ["@@doublez__heye-turn"] = "合野",

  ["$doublez__heye1"] = "此征艰险，万事小心为慎。",
  ["$doublez__heye2"] = "苦海茫茫，渡心无边。",
}

heye:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return end
    local change = 0
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Player.Equip then
        change = change + #move.moveInfo
      elseif move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Player.Equip then
            change = change - 1
          end
        end
      end
    end
    return change ~= 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = "iron_chain",
      skill_name = self.name,
      skip = true
    })
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self) ---@type UseCardDataSpec
    room:useCard(use) 
    if player.dead then return end
    local only = {player}
    table.insertIfNeed(only, player:getLastAlive())
    table.insertIfNeed(only, player:getNextAlive())
    for _, to in ipairs(use.tos) do
      if not table.contains(only, to) then
        return
      end
    end
    room:setPlayerMark(player, "@@doublez__heye-turn", 1)
  end
})

heye:addEffect("viewas", {
  pattern = "slash,jink",
  card_filter = Util.FalseFunc,
  interaction = function(self, player)
    local names = player:getViewAsCardNames(heye.name, {"slash", "jink"})
    return UI.CardNameBox {choices = names, all_choices = {"slash", "jink"}}
  end,
  view_as = function (self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = heye.name
    return card
  end,
  enabled_at_play = function (self, player)
    return player:getMark("@@doublez__heye-turn") > 0
  end,
  enabled_at_response = function (self, player, response)
    return player:getMark("@@doublez__heye-turn") > 0 and not response
  end,
  before_use = function (self, player, use)
    player.room:removePlayerMark(player, "@@doublez__heye-turn", 1)
  end
})

return heye